const inventory = [
  { name: "芦笋", type: "蔬菜", quantity: 5 },
  { name: "香蕉", type: "水果", quantity: 0 },
  { name: "山羊", type: "肉", quantity: 23 },
  { name: "樱桃", type: "水果", quantity: 5 },
  { name: "鱼", type: "肉", quantity: 22 },
];


const result = Object.groupBy(inventory, ({ type }) => type);

console.log('result :>> ', result);


function groupByKey(data, key) {
  return data.reduce((acc, info) => {
    // 提取文件扩展名（不包括点）
    const extension = info[key] || "no";
    if (!acc[extension]) {
      acc[extension] = [];
    }
    acc[extension].push(info);
    return acc;
  }, {});
}

console.log('groupByKey :>> ', groupByKey(inventory, 'type'));